import axios, { InternalAxiosRequestConfig, AxiosResponse } from 'axios';

// 创建axios实例
const request = axios.create({
  baseURL: 'https://api.example.com',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 请求拦截器 - 使用正确的类型
request.interceptors.request.use(
  (config: InternalAxiosRequestConfig): InternalAxiosRequestConfig => {
    const token = localStorage.getItem('token');
    if (token && config.headers) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error: any) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
request.interceptors.response.use(
  (response: AxiosResponse): any => {
    return response.data;
  },
  (error: any) => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          // 处理未授权错误
          break;
        case 404:
          // 处理资源不存在错误
          break;
        default:
          // 处理其他错误
      }
    }
    return Promise.reject(error);
  }
);

export default request;